package com.yanqu.road.server.gameplayer.module.activity.daomu;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.daomu.DaoMuBattlePropItem;
import com.yanqu.road.entity.activity.daomu.DaoMuReward;
import com.yanqu.road.entity.activity.daomu.DaoMuSkill;
import com.yanqu.road.entity.activity.daomu.config.DaoMuActivityConfig;
import com.yanqu.road.entity.activity.daomu.config.DaoMuCrowbarBoxConfig;
import com.yanqu.road.entity.activity.daomu.config.DaoMuMissionConfig;
import com.yanqu.road.entity.activity.daomu.config.DaoMuSkillConfig;
import com.yanqu.road.entity.activity.daomu.data.DaoMuActivityUserCollectionData;
import com.yanqu.road.entity.activity.daomu.data.DaoMuActivityUserData;
import com.yanqu.road.entity.activity.daomu.data.DaoMuActivityUserOfferRewardData;
import com.yanqu.road.entity.activity.daomu.enums.DaoMuActivityChildTypeEnum;
import com.yanqu.road.entity.activity.daomu.enums.DaoMuNpcStatusEnum;
import com.yanqu.road.entity.activity.daomu.enums.DaoMuSyncConfigTypeEnum;
import com.yanqu.road.entity.activity.daomu.param.DaoMuAttReportRes;
import com.yanqu.road.entity.activity.daomu.param.DaoMuCreateNpcRes;
import com.yanqu.road.entity.activity.daomu.param.DaoMuOpenBoxRes;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouRoundConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.messageSubscribe.entity.MessageUserNoticeData;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.bussiness.activity.DaoMuActivityBusiness;
import com.yanqu.road.logic.bussiness.activity.JiuZhouBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.pb.activity.DaoMuProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.BadgeModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.logic.activity.condition.daomu.DaoMuPvePersonRankArgs;
import com.yanqu.road.server.manger.activity.daomu.DaoMuActivityMgr;
import com.yanqu.road.server.manger.activity.daomu.DaoMuActivityPveRoom;
import com.yanqu.road.server.manger.activity.daomu.DaoMuActivityUser;
import com.yanqu.road.server.manger.activity.daomu.pb.DaoMuActivityPb;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.messagesubscribe.MessageSubscribeMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DaoMuActivityModule extends GeneralModule {

    private Map<Integer, DaoMuActivityUser> daoMuActivityUserMap = new ConcurrentHashMap<>();

    public DaoMuActivityModule(GamePlayer player) {
        super(player);
    }

    public DaoMuActivityUser getDaoMuActivityUser(int activityId) {
        return daoMuActivityUserMap.get(activityId);
    }

    @Override
    public boolean loadData() {
        for (DaoMuActivityConfig daoMuActivityConfig : DaoMuActivityMgr.getActivityConfigMap().values()) {
            //加载玩家盗墓数据
            DaoMuActivityUserData daoMuActivityUserData = DaoMuActivityBusiness.getDaoMuActivityUserDataInfo(daoMuActivityConfig.getActivityId(), this.getUserId());
            //加载藏品数据
            Map<Integer, DaoMuActivityUserCollectionData> collectionData = DaoMuActivityBusiness.getDaoMuActivityUserCollectionData(daoMuActivityConfig.getActivityId(), this.getUserId());
            //加载玩家悬赏数据
            Map<Integer, Map<Integer, DaoMuActivityUserOfferRewardData>> offerRewardMap = DaoMuActivityBusiness.getDaoMuActivityUserOfferRewardData(daoMuActivityConfig.getActivityId(), this.getUserId());
            if (daoMuActivityUserData != null) {
                //玩家对象
                DaoMuActivityUser daoMuActivityUser = new DaoMuActivityUser(player, daoMuActivityConfig, daoMuActivityUserData, collectionData, offerRewardMap);
                this.daoMuActivityUserMap.put(daoMuActivityConfig.getActivityId(), daoMuActivityUser);
            }
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public void loginSendMsg() {
        syncConfig(DaoMuSyncConfigTypeEnum.SYNC_CONFIG_TYPE_DAOMU_SPECIAL);
    }

    @Override
    public boolean saveData() {
        for (DaoMuActivityUser daoMuActivityUser : this.daoMuActivityUserMap.values()) {
            daoMuActivityUser.saveData();
        }
        return true;
    }

    @Override
    public void afterLogin() {
        //同步配置信息
        syncConfig(DaoMuSyncConfigTypeEnum.SYNC_CONFIG_TYPE_DAOMU);
        //特色服提前创建玩家数据
        for (DaoMuActivityConfig daoMuActivityConfig : DaoMuActivityMgr.getActivityConfigMap().values()) {
            if (daoMuActivityConfig.getActivityInfo().getType() == eActivityType.DaoMuSpecial.getValue()) {
                DaoMuActivityUser daoMuActivityUser = this.daoMuActivityUserMap.get(daoMuActivityConfig.getActivityId());
                if (daoMuActivityUser == null) {
                    createPlayer(daoMuActivityConfig);
                }
            }
        }
        //同步盗墓玩家信息
        syncPlayerInfo();
    }

    /**
     * 同步配置
     */
    public void syncConfig(DaoMuSyncConfigTypeEnum daoMuSyncConfigTypeEnum) {
        for (DaoMuActivityConfig daoMuActivityConfig : DaoMuActivityMgr.getActivityConfigMap().values()) {
            if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
                continue;
            }
            DaoMuActivityUser daoMuActivityUser = this.daoMuActivityUserMap.get(daoMuActivityConfig.getActivityId());
            if (daoMuActivityUser != null) {
                daoMuActivityUser.setDaoMuActivityConfig(daoMuActivityConfig);
            }
            //盗墓笔记
            if (daoMuSyncConfigTypeEnum.getType() == DaoMuSyncConfigTypeEnum.SYNC_CONFIG_TYPE_DAOMU.getType() ||
                    daoMuSyncConfigTypeEnum.getType() == DaoMuSyncConfigTypeEnum.SYNC_CONFIG_TYPE_ALL.getType()){
                if (daoMuActivityConfig.getActivityInfo().getType() == eActivityType.DaoMu.getValue()) {
                    DaoMuProto.DaoMuActivityConfig.Builder syncBuilder = DaoMuActivityMgr.getActivityConfigMsg(daoMuActivityConfig, player.getLanguage());
                    player.sendPacket(Protocol.U_DAOMU_CONFIG_SYNC, syncBuilder);
                }
            }
            //盗墓奇遇
            if (daoMuSyncConfigTypeEnum.getType() == DaoMuSyncConfigTypeEnum.SYNC_CONFIG_TYPE_DAOMU_SPECIAL.getType() ||
                    daoMuSyncConfigTypeEnum.getType() == DaoMuSyncConfigTypeEnum.SYNC_CONFIG_TYPE_ALL.getType()){
                if (daoMuActivityConfig.getActivityInfo().getType() == eActivityType.DaoMuSpecial.getValue()) {
//                    DaoMuProto.DaoMuActivityConfig.Builder syncBuilder = DaoMuActivityPb.syncConfig(daoMuActivityConfig, player.getLanguage());
//                    player.sendPacket(Protocol.U_DAOMU_SPECIAL_CONFIG_SYNC, syncBuilder);
                    player.sendPacket(Protocol.U_DAOMU_SPECIAL_CONFIG_SYNC, DaoMuProto.DaoMuActivityConfig.newBuilder());
                }
            }
        }

        // 设置微信推送时间
        try {
            MessageSubscribePushTime();
        }catch (Exception e){
            log.error("error {}", e);
        }
    }

    /**
     * 设置推送时间
     */
    private void MessageSubscribePushTime(){
        if(!MessageSubscribeMgr.isSwitch()){
            return;
        }

        List<Long> timeBattleList = new ArrayList<>();
        for (DaoMuActivityConfig daoMuActivityConfig : DaoMuActivityMgr.getActivityConfigMap().values()) {
            if (daoMuActivityConfig == null || !ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
                continue;
            }
            ActivityInfo daoMuAct = daoMuActivityConfig.getActivityInfo();
            if (daoMuAct == null) {
                continue;
            }
            if (daoMuActivityConfig.getPvpCommonConfig() == null) {
                continue;
            }
            long beginTime = DateHelper.getZeroTimeStamp(daoMuAct.getBeginTime() * 1000) / 1000;
            long endTime = DateHelper.getZeroTimeStamp((daoMuAct.getEndTime() - daoMuActivityConfig.getPvpCommonConfig().getPvpContinuedEndTime()) * 1000) / 1000;
            for (int i = 0; i <= (endTime - beginTime) / DateHelper.DAY_SECONDS; i++){
                long battleTime = DateHelper.getZeroTimeStamp((beginTime + i * DateHelper.DAY_SECONDS) * DateHelper.SECOND_MILLIONS) + daoMuActivityConfig.getPvpCommonConfig().getPvpContinuedStartTime() * DateHelper.SECOND_MILLIONS - 5 * 60 * DateHelper.SECOND_MILLIONS;
                if (battleTime <= 0) {
                    continue;
                }
                timeBattleList.add(battleTime);
            }
        }
        MessageUserNoticeData noticeData = MessageSubscribeMgr.getUserNoticeData(player.getUserId());
        noticeData.setSubTime(eMessageSubscribeType.DaoMuTime, timeBattleList);
    }

    /**
     * 同步玩家信息
     */
    public void syncPlayerInfo() {
        for (DaoMuActivityConfig daoMuActivityConfig : DaoMuActivityMgr.getActivityConfigMap().values()) {
            DaoMuActivityUser daoMuActivityUser = this.daoMuActivityUserMap.get(daoMuActivityConfig.getActivityId());
            if (daoMuActivityUser != null) {
                daoMuActivityUser.syncPlayInfoMsg();
            }
        }
    }

    /**
     * 进入活动
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuEnterActivityRespMsg.Builder enterActivity(DaoMuProto.DaoMuEnterActivityReqMsg reqMsg) {
        DaoMuProto.DaoMuEnterActivityRespMsg.Builder respMsg = DaoMuProto.DaoMuEnterActivityRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        DaoMuActivityUser activityUser = daoMuActivityUserMap.get(activityId);
        if (null == activityUser) {
            activityUser = createPlayer(daoMuActivityConfig);
        }
        respMsg.setRet(0);
        respMsg.setPlayerInfo(DaoMuActivityPb.createPlayerInfoPb(activityUser));
        return respMsg;
    }

    /**
     * 选择角色
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuSelectRoleRespMsg.Builder selectRole(DaoMuProto.DaoMuSelectRoleReqMsg reqMsg) {
        DaoMuProto.DaoMuSelectRoleRespMsg.Builder respMsg = DaoMuProto.DaoMuSelectRoleRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断是否在活动进行期
        if (!ActivityHelper.activityInTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_IN_TIME);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        try {
            //选择角色
            daoMuActivityUser.selectRole(reqMsg.getRoleId());
            respMsg.setRet(0);
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        return respMsg;
    }

    /**
     * 藏品升级
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuCollectionUpgradeRespMsg.Builder collectionUpgrade(DaoMuProto.DaoMuCollectionUpgradeReqMsg reqMsg) {
        DaoMuProto.DaoMuCollectionUpgradeRespMsg.Builder respMsg = DaoMuProto.DaoMuCollectionUpgradeRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        //藏品升级
        try {
            daoMuActivityUser.collectionUpgrade(reqMsg.getCollectionId(), true);
            respMsg.setRet(0);
            respMsg.setPlayerInfo(DaoMuActivityPb.createPlayerInfoPb(daoMuActivityUser));
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        return respMsg;
    }

    /**
     * 藏品升级
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuBatchCollectionUpgradeRespMsg.Builder batchCollectionUpgrade(DaoMuProto.DaoMuBatchCollectionUpgradeReqMsg reqMsg) {
        DaoMuProto.DaoMuBatchCollectionUpgradeRespMsg.Builder respMsg = DaoMuProto.DaoMuBatchCollectionUpgradeRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        //藏品升级-批量
        daoMuActivityUser.batchCollectionUpgrade();
        respMsg.setRet(0);
        respMsg.setPlayerInfo(DaoMuActivityPb.createPlayerInfoPb(daoMuActivityUser));
        return respMsg;
    }

    /**
     * 盗墓笔记-使用道具恢复体力-废弃
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuUsePropRecoverEnergyRespMsg.Builder usePropRecoverEnergy(DaoMuProto.DaoMuUsePropRecoverEnergyReqMsg reqMsg) {
        DaoMuProto.DaoMuUsePropRecoverEnergyRespMsg.Builder respMsg = DaoMuProto.DaoMuUsePropRecoverEnergyRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取体力包ID
        String energyPropIdStr = daoMuActivityConfig.getDaoMuActivityConfigValue(eActivityConfigType.DAOMU_ENERGY_ITEM_ID);
        int energyPropId = Integer.valueOf(energyPropIdStr);
        Property property = PropertyHelper.parseStringToProperty(reqMsg.getProp());
        if (property == null || !property.getGoods().containsKey(energyPropId)) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PROP_ERROR);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        //消耗道具
        if (!player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.DaoMu, eLogMoneyType.DaoMuUsePropRecoverEnergy)) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PROP_NOT_ENOUGH);
            return respMsg;
        }
        int energy = daoMuActivityUser.usePropRecoverEnergy(reqMsg.getProp());
        respMsg.setRet(0);
        respMsg.setEnergy(energy);
        return respMsg;
    }

    /**
     * 摸金
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuTouchGoldRespMsg.Builder touchGold(DaoMuProto.DaoMuTouchGoldReqMsg reqMsg) {
        DaoMuProto.DaoMuTouchGoldRespMsg.Builder respMsg = DaoMuProto.DaoMuTouchGoldRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        DaoMuCrowbarBoxConfig daoMuCrowbarBoxConfig = daoMuActivityConfig.getCrowbarBoxConfigMap().get(reqMsg.getBoxId());
        if (daoMuCrowbarBoxConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_TOUCH_GOLD_BOX_NOT_EXIST_ERROR);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        //获取消耗道具
        Property cost = PropertyHelper.parseStringToProperty(daoMuCrowbarBoxConfig.getCost());
        cost.rideProperty(reqMsg.getNum());
        //消耗道具
        if (!player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.DaoMu, eLogMoneyType.DaoMuTouchGoldReProp)) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PROP_NOT_ENOUGH);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //摸金
            DaoMuReward daoMuReward = daoMuActivityUser.touchGold(reqMsg.getBoxId(), reqMsg.getNum());
            //返回PB
            respMsg.setRet(0);
            respMsg.setPlayerInfo(DaoMuActivityPb.createPlayerInfoPb(daoMuActivityUser));
            respMsg.setReward(PropertyHelper.parsePropertyToString(daoMuReward.getReward()));
            respMsg.setActivityReward(PropertyHelper.parsePropertyToString(daoMuReward.getActivityReward()));
            return respMsg;
        }
    }

    /**
     * 一键摸金
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuTouchGoldRespMsg.Builder touchGoldOneKey(DaoMuProto.DaoMuTouchGoldReqMsg reqMsg) {
        DaoMuProto.DaoMuTouchGoldRespMsg.Builder respMsg = DaoMuProto.DaoMuTouchGoldRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }

        StringBuilder reward = new StringBuilder();
        StringBuilder actReward = new StringBuilder();
        for (DaoMuCrowbarBoxConfig daoMuCrowbarBoxConfig : daoMuActivityConfig.getCrowbarBoxConfigMap().values()) {
            if (daoMuCrowbarBoxConfig == null) {
                continue;
            }
            //获取消耗道具
            Property cost = PropertyHelper.parseStringToProperty(daoMuCrowbarBoxConfig.getCost());
            if (cost.isNothing()) {
                continue;
            }

            long num = 0;
            for (Map.Entry<Integer, BigInteger> entry : cost.getGoods().entrySet()) {
                int goodsId = entry.getKey();
                num = player.getModule(BagModule.class).getUserBagItemCount(goodsId);
                if (num <= 0) {
                    continue;
                }
                cost.rideProperty(num);
                // 策划确定只有一个道具，即对应的宝箱，所以此处直接跳出
                break;
            }
            //消耗道具
            if (!player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.DaoMu, eLogMoneyType.DaoMuTouchGoldReProp)) {
                continue;
            }
            synchronized (daoMuActivityUser) {
                //摸金
                DaoMuReward daoMuReward = daoMuActivityUser.touchGold(daoMuCrowbarBoxConfig.getId(), num);
                if (reward.toString().isEmpty()) {
                    reward.append(PropertyHelper.parsePropertyToString(daoMuReward.getReward()));
                } else {
                    reward.append(";").append(PropertyHelper.parsePropertyToString(daoMuReward.getReward()));
                }
                if (actReward.toString().isEmpty()) {
                    actReward.append(PropertyHelper.parsePropertyToString(daoMuReward.getActivityReward()));
                } else {
                    actReward.append(";").append(PropertyHelper.parsePropertyToString(daoMuReward.getActivityReward()));
                }
            }
        }
        //返回PB
        respMsg.setRet(0);
        respMsg.setPlayerInfo(DaoMuActivityPb.createPlayerInfoPb(daoMuActivityUser));
        respMsg.setReward(reward.toString());
        respMsg.setActivityReward(actReward.toString());
        return respMsg;
    }

    /**
     * 解锁角色
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuUnlockRoleRespMsg.Builder unlockRole(DaoMuProto.DaoMuUnlockRoleReqMsg reqMsg) {
        DaoMuProto.DaoMuUnlockRoleRespMsg.Builder respMsg = DaoMuProto.DaoMuUnlockRoleRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断是否在活动进行期
        if (!ActivityHelper.activityInTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_IN_TIME);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        //判断道具类型是【盗墓笔记】【摸金觅宝】角色类型
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(reqMsg.getRolePropId());
        int roleGoodsType = daoMuActivityConfig.getRoleGoodsType();
        if (goodsInfo == null || goodsInfo.getType() != roleGoodsType) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PROP_ERROR);
            return respMsg;
        }
        //消耗道具
        Property property = new Property(reqMsg.getRolePropId(), BigInteger.ONE);
        if (!player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.DaoMu, eLogMoneyType.DaoMuUnlockRoleReProp)) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PROP_NOT_ENOUGH);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //解锁角色
            int roleId = daoMuActivityUser.unlockRole(reqMsg.getRolePropId());
            int num = daoMuActivityUser.receiveRoleUnlockJadeBf(roleId);
            //返回PB
            respMsg.setRet(0);
            respMsg.setPlayerInfo(DaoMuActivityPb.createPlayerInfoPb(daoMuActivityUser));
            respMsg.setBfJadeNum(num);
            return respMsg;
        }
    }

    /**
     * 开启摸金宝箱
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuOpenProgressBoxRespMsg.Builder openTouchGoldProgressBox(DaoMuProto.DaoMuOpenProgressBoxReqMsg reqMsg) {
        DaoMuProto.DaoMuOpenProgressBoxRespMsg.Builder respMsg = DaoMuProto.DaoMuOpenProgressBoxRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            Property reward = daoMuActivityUser.openAllTouchGoldProgressBox();
            respMsg.setRet(0);
            respMsg.setReward(PropertyHelper.parsePropertyToString(reward));
            respMsg.setPlayerInfo(DaoMuActivityPb.createPlayerInfoPb(daoMuActivityUser));
            return respMsg;
        }
    }

    /**
     * 获取悬赏奖励
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuOfferRewardRespMsg.Builder getOfferReward(DaoMuProto.DaoMuOfferRewardReqMsg reqMsg) {
        DaoMuProto.DaoMuOfferRewardRespMsg.Builder respMsg = DaoMuProto.DaoMuOfferRewardRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            Map<Integer, DaoMuActivityUserOfferRewardData> map = daoMuActivityUser.getOfferReward(reqMsg.getPasserId());
            respMsg.setRet(0);
            if (map != null) {
                for (DaoMuActivityUserOfferRewardData offerRewardData : map.values()) {
                    respMsg.addOfferReward(DaoMuActivityPb.createOfferRewardItemPb(offerRewardData));
                }
            }
            return respMsg;
        }
    }

    /**
     * 领取悬赏奖励
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuReceiveOfferRewardRespMsg.Builder receiveOfferReward(DaoMuProto.DaoMuReceiveOfferRewardReqMsg reqMsg) {
        DaoMuProto.DaoMuReceiveOfferRewardRespMsg.Builder respMsg = DaoMuProto.DaoMuReceiveOfferRewardRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            try {
                DaoMuReward daoMuReward = daoMuActivityUser.receiveOfferReward(reqMsg.getPasserId(), reqMsg.getNpcId());
                respMsg.setRet(0);
                respMsg.setReward(PropertyHelper.parsePropertyToString(daoMuReward.getReward()));
                respMsg.setActivityReward(PropertyHelper.parsePropertyToString(daoMuReward.getActivityReward()));
                respMsg.setPlayerInfo(DaoMuActivityPb.createPlayerInfoPb(daoMuActivityUser));
                player.getModule(CurrencyModule.class).addCurrency(daoMuReward.getReward(), eLogMoneyType.DaoMu, eLogMoneyType.DaoMuReceiveOfferReward);
            } catch (BusinessException e) {
                respMsg.setRet(e.getCode());
            }
            return respMsg;
        }
    }

    /**
     * 快速过关
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuQuickPassRespMsg.Builder quickPass(DaoMuProto.DaoMuQuickPassReqMsg reqMsg) {
        DaoMuProto.DaoMuQuickPassRespMsg.Builder respMsg = DaoMuProto.DaoMuQuickPassRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInShowTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            try {
                DaoMuProto.DaoMuStatementInfoItem.Builder statement = daoMuActivityUser.quickPass(reqMsg.getPasserId());
                respMsg.setRet(0);
                respMsg.setStatement(statement);
            } catch (BusinessException e) {
                respMsg.setRet(e.getCode());
            }
            return respMsg;
        }
    }

    /**
     * 创建盗墓玩家
     * @param daoMuActivityConfig
     * @return
     */
    public DaoMuActivityUser createPlayer(DaoMuActivityConfig daoMuActivityConfig) {
        synchronized (this) {
            DaoMuActivityUser daoMuActivityUser = this.daoMuActivityUserMap.get(daoMuActivityConfig.getActivityId());
            if (daoMuActivityUser != null) {
                return daoMuActivityUser;
            }
            //默认角色
            String defaultRoleIdStr = daoMuActivityConfig.getDaoMuActivityConfigValue(eActivityConfigType.DAOMU_PARTNER_INITIAL_UNLOCK_ITEM_ID);
            int defaultRoleId = Integer.parseInt(defaultRoleIdStr);
            //默认进度宝箱ID
            int progressBoxId = daoMuActivityConfig.getDefaultProgressBoxId();
            //创建玩家
            DaoMuActivityUserData userData = new DaoMuActivityUserData();
            userData.setActivityId(daoMuActivityConfig.getActivityId());
            userData.setUserId(player.getUserId());
            userData.setPassesId(daoMuActivityConfig.getDefaultPassesId());//默认关卡
            userData.setPassPrePassesTime(0);
            userData.setEndlessWave(0);
            userData.setEnergy(0);//体力初始值
            userData.setCreateTime(System.currentTimeMillis());
            userData.setLastRestoreTime(0);
            userData.setServerId(GameServer.getInstance().getServerId());
            userData.setProgressBoxId(progressBoxId);
            userData.setBoxProgress(0);
            userData.setPassAllFlag(0);
            userData.setHistoryMaxPassesId(0);
            userData.setInsertOption();
            //实例化盗墓玩家对象
            Map<Integer, DaoMuActivityUserCollectionData> collectionDataMap = new ConcurrentHashMap<>();
            Map<Integer, Map<Integer, DaoMuActivityUserOfferRewardData>> offerRewardMap = new ConcurrentHashMap<>();
            DaoMuActivityUser activityUser = new DaoMuActivityUser(player, daoMuActivityConfig, userData, collectionDataMap, offerRewardMap);
            if (daoMuActivityConfig.getActivityInfo().getType() == eActivityType.DaoMu.getValue()) {
                //解锁角色
                activityUser.unlockRole(defaultRoleId);
                //解锁默认藏品
                activityUser.unlockDefaultCollection();
                //赠送宝箱
                String zsBox = daoMuActivityConfig.getDaoMuActivityConfigValue(eActivityConfigType.DAOMU_GUIDE_BOX_ITEM_ID);
                Property zsBoxProp = PropertyHelper.parseStringToProperty(zsBox);
                player.getModule(CurrencyModule.class).addCurrency(zsBoxProp, eLogMoneyType.DaoMu, eLogMoneyType.DaoMuStartZsBox);
            }
            userData.setRoleId(defaultRoleId);//默认角色

            daoMuActivityUserMap.put(activityUser.getActivityId(), activityUser);
            return activityUser;
        }
    }

    /************************************************************PVE**********************************************************/

    /**
     * PVE-探索
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuPveExploreRespMsg.Builder pveExplore(DaoMuProto.DaoMuPveExploreReqMsg reqMsg) {
        DaoMuProto.DaoMuPveExploreRespMsg.Builder respMsg = DaoMuProto.DaoMuPveExploreRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断活动是否存在
        if (!ActivityHelper.activityInTime(daoMuActivityConfig.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //判断关卡是否存在
        DaoMuMissionConfig daoMuMissionConfig = daoMuActivityConfig.getDaoMuMissionConfigMap().get(reqMsg.getPassesId());
        if (daoMuMissionConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PASSES_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            try {
                //探索
                DaoMuActivityPveRoom room = daoMuActivityUser.pveExplore(reqMsg.getPassesId());
                respMsg.setRet(0);
                respMsg.setRoomId(room.getRoomId());
                for (DaoMuBattlePropItem daoMuBattlePropItem : room.getBattlePropMap().values()) {
                    respMsg.addBattlePropList(DaoMuActivityPb.createDaoMuBattlePropItem(daoMuBattlePropItem));
                }
                respMsg.setPlayerInfo(DaoMuActivityPb.createPlayerInfoPb(daoMuActivityUser));
                respMsg.setRoomPlayer(DaoMuActivityPb.createPveRoomPlayerPb(room));
            } catch (BusinessException e) {
                respMsg.setRet(e.getCode());
            }
            return respMsg;
        }
    }

    /**
     * PVE-刷新NPC
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuPveRefreshNpcRespMsg.Builder pveRefreshNpc(DaoMuProto.DaoMuPveRefreshNpcReqMsg reqMsg) {
        DaoMuProto.DaoMuPveRefreshNpcRespMsg.Builder respMsg = DaoMuProto.DaoMuPveRefreshNpcRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            try {
                //创建NPC
                DaoMuCreateNpcRes daoMuCreateNpc = room.createNpc(reqMsg.getSecond());
                //返回PB
                return DaoMuActivityPb.createPveRefreshNpcRespMsgPb(daoMuCreateNpc);
            } catch (BusinessException e) {
                respMsg.setRet(e.getCode());
            }
            return respMsg;
        }
    }

    /**
     * PVE-普通NPC伤害上报
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuPveKillNpcRespMsg.Builder pveKillNpc(DaoMuProto.DaoMuPveKillNpcReqMsq reqMsg) {
        DaoMuProto.DaoMuPveKillNpcRespMsg.Builder respMsg = DaoMuProto.DaoMuPveKillNpcRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            //普通NPC伤害上报
            room.attNpc(reqMsg.getAttNpcListList());
            //悬赏奖励领取，通知客户端
            daoMuActivityUser.saveAndReceiveOfferReward(room.getPassesId(), room.getOfferRewardNpcIdSet(), room.getRoomId());
            //返回PB
            respMsg.setRet(0);
            return respMsg;
        }
    }

    /**
     * PVE-升级校验
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuPveUpgradeRespMsg.Builder pveUpgrade(DaoMuProto.DaoMuPveUpgradeReqMsg reqMsg) {
        DaoMuProto.DaoMuPveUpgradeRespMsg.Builder respMsg = DaoMuProto.DaoMuPveUpgradeRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            //升级校验
            List<DaoMuSkill> selectSkillList = room.upgrade(reqMsg.getExp(), reqMsg.getExpStoneListList(), reqMsg.getMillisecond());
            //返回PB
            respMsg.setRoomPlayer(DaoMuActivityPb.createPveRoomPlayerPb(room));
            if (selectSkillList != null) {
                for (DaoMuSkill daoMuSkill : selectSkillList) {
                    //获取觉醒配置
                    DaoMuSkillConfig daoMuSkillConfig = room.getRealDaoMuSkillConfig(daoMuSkill);
                    DaoMuProto.DaoMuSkillItem skillItem = DaoMuActivityPb.createDaoMuSkillItem(daoMuSkill, daoMuSkillConfig);
                    respMsg.addSkillList(skillItem);
                }
            }
            respMsg.setRet(0);
            return respMsg;
        }
    }

    /**
     * PVE-重随技能
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuRandomSelectSkillListRespMsg.Builder pveRandomSelectSkillList(DaoMuProto.DaoMuRandomSelectSkillListReqMsg reqMsg) {
        DaoMuProto.DaoMuRandomSelectSkillListRespMsg.Builder respMsg = DaoMuProto.DaoMuRandomSelectSkillListRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            //重随技能
            try {
                List<DaoMuSkill> selectSkillList = room.randomSelectSkillList(this.player);
                //返回PB
                respMsg.setRet(0);
                respMsg.setRoomPlayer(DaoMuActivityPb.createPveRoomPlayerPb(room));
                if (selectSkillList != null) {
                    for (DaoMuSkill daoMuSkill : selectSkillList) {
                        //获取觉醒配置
                        DaoMuSkillConfig daoMuSkillConfig = room.getRealDaoMuSkillConfig(daoMuSkill);
                        DaoMuProto.DaoMuSkillItem skillItem = DaoMuActivityPb.createDaoMuSkillItem(daoMuSkill, daoMuSkillConfig);
                        respMsg.addSkillList(skillItem);
                    }
                }
            } catch (BusinessException e) {
                respMsg.setRet(e.getCode());
            }
            return respMsg;
        }
    }

    /**
     * PVE-升级选择技能
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuPveUpgradeSelectSkillRespMsg.Builder pveUpgradeSelectSkill(DaoMuProto.DaoMuPveUpgradeSelectSkillReqMsg reqMsg) {
        DaoMuProto.DaoMuPveUpgradeSelectSkillRespMsg.Builder respMsg = DaoMuProto.DaoMuPveUpgradeSelectSkillRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            //选择技能
            try {
                room.upgradeSelectSkill(reqMsg.getSkillId(), reqMsg.getMillisecond());
                //返回PB
                respMsg.setRoomPlayer(DaoMuActivityPb.createPveRoomPlayerPb(room));
                respMsg.setRet(0);
            } catch (BusinessException e) {
                respMsg.setRet(e.getCode());
            }
            return respMsg;
        }
    }

    /**
     * PVE-攻击上报（每秒上报【攻击BOSS】,【被攻击】伤害信息）
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuPveAttReportRespMsq.Builder pveAttReport(DaoMuProto.DaoMuPveAttReportReqMsq reqMsg) {
        DaoMuProto.DaoMuPveAttReportRespMsq.Builder respMsg = DaoMuProto.DaoMuPveAttReportRespMsq.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            if (room.getDaoMuStatementInfo() != null) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            //攻击上报
            DaoMuAttReportRes result = room.attReport(reqMsg.getAttBossListList(), reqMsg.getBeAttListList());
            //返回PB
            respMsg.setRet(0);
            respMsg.setRoomPlayer(DaoMuActivityPb.createPveRoomPlayerPb(room));
            respMsg.setPassesId(room.getPassesId());
            //boss数据
            if (result.getBoss() != null) {
                respMsg.setKillFlag(result.getBoss().getStatus() != DaoMuNpcStatusEnum.NPC_STATUS_EXIST.getStatus());
                respMsg.setReduceHp(result.getBoss().getReHp());
                //悬赏奖励领取，通知客户端
                daoMuActivityUser.saveAndReceiveOfferReward(room.getPassesId(), room.getOfferRewardNpcIdSet(), room.getRoomId());
            }
            //判断是否结算结算
            if (room.getDaoMuStatementInfo() != null) {
                //结算flag
                boolean statementFlag = true;
                //判断是特殊服,非第一关，才有广告
                if (daoMuActivityConfig.getActivityInfo().getType() == eActivityType.DaoMuSpecial.getValue()
                        && !room.getDaoMuStatementInfo().isStatus() && daoMuActivityConfig.getDefaultPassesId() != room.getPassesId()) {
                    //盗墓奇遇死亡不直接结算,可看广告复活1次
                    if (room.getResurgenceNum() <= 0) {
                        DaoMuProto.DaoMuStatementInfoItem.Builder statement = DaoMuProto.DaoMuStatementInfoItem.newBuilder();
                        statement.setStatus(false);
                        statement.setAdvFlag(true);
                        respMsg.setStatement(statement);
                        statementFlag = false;
                    }
                }
                //结算
                if (statementFlag) {
                    //结算
                    DaoMuProto.DaoMuStatementInfoItem.Builder statement = daoMuActivityUser.overPasses(room.getPassesId(), room.getDaoMuStatementInfo().isStatus(), room.getOfferRewardNpcIdSet(),
                            room.getLevel(), room.getSkillIds(), room.getDaoMuStatementInfo().getBattleTime(), room.getDaoMuStatementInfo().getKillNum(), room.getRoomId());
                    respMsg.setStatement(statement);
                    respMsg.setOverFlag(true);
                }
            }
            return respMsg;
        }
    }

    /**
     * PVE-打破瓦罐
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuPveBreakClayRespMsg.Builder pveBreakClay(DaoMuProto.DaoMuPveBreakClayReqMsg reqMsg) {
        DaoMuProto.DaoMuPveBreakClayRespMsg.Builder respMsg = DaoMuProto.DaoMuPveBreakClayRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            //打破瓦罐
            try {
                Map<Integer, DaoMuBattlePropItem> propItemMap = room.breakClay(reqMsg.getBattlePropId());
                //返回PB
                respMsg.setRet(0);
                for (DaoMuBattlePropItem daoMuBattlePropItem : propItemMap.values()) {
                    respMsg.addBattlePropList(DaoMuActivityPb.createDaoMuBattlePropItem(daoMuBattlePropItem));
                }
            } catch (BusinessException e) {
//                e.printStackTrace();
                respMsg.setRet(e.getCode());
            }
            return respMsg;
        }
    }

    /**
     * PVE-捡道具
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuPvePickUpPropRespMsg.Builder pvePickUpProp(DaoMuProto.DaoMuPvePickUpPropReqMsg reqMsg) {
        DaoMuProto.DaoMuPvePickUpPropRespMsg.Builder respMsg = DaoMuProto.DaoMuPvePickUpPropRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            //捡道具
            try {
                //捡起道具
                room.pickUpProp(reqMsg.getBattlePropId());
                //返回PB
                respMsg.setRoomPlayer(DaoMuActivityPb.createPveRoomPlayerPb(room));
                respMsg.setRet(0);
            } catch (BusinessException e) {
                respMsg.setRet(e.getCode());
            }
            return respMsg;
        }
    }

    /**
     * PVE-捡取宝箱
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuPvePickUpBoxRespMsg.Builder pvePickUpBox(DaoMuProto.DaoMuPvePickUpBoxReqMsg reqMsg) {
        DaoMuProto.DaoMuPvePickUpBoxRespMsg.Builder respMsg = DaoMuProto.DaoMuPvePickUpBoxRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            try {
                //捡起宝箱
                DaoMuOpenBoxRes daoMuOpenBoxRes = room.pickUpBox(reqMsg.getBattlePropId(), reqMsg.getMillisecond());
                //返回PB
                respMsg.setRet(0);
                respMsg.setRoomPlayer(DaoMuActivityPb.createPveRoomPlayerPb(room));
                for (DaoMuSkill daoMuSkill : daoMuOpenBoxRes.getAllSkillList()) {
                    //获取觉醒配置
                    DaoMuSkillConfig daoMuSkillConfig = room.getRealDaoMuSkillConfig(daoMuSkill);
                    respMsg.addSkillList(DaoMuActivityPb.createDaoMuSkillItem(daoMuSkill, daoMuSkillConfig));
                }
                for (DaoMuSkill daoMuSkill : daoMuOpenBoxRes.getSelectedSkillList()) {
                    //获取觉醒配置
                    DaoMuSkillConfig daoMuSkillConfig = room.getRealDaoMuSkillConfig(daoMuSkill);
                    respMsg.addSelectedSkillList(DaoMuActivityPb.createDaoMuSkillItem(daoMuSkill, daoMuSkillConfig));
                }
            } catch (BusinessException e) {
                respMsg.setRet(e.getCode());
            }
            return respMsg;
        }
    }

    /**
     * PVE-生成陶罐
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuCreateClayRespMsg.Builder pveCreateClay(DaoMuProto.DaoMuCreateClayReqMsg reqMsg) {
        DaoMuProto.DaoMuCreateClayRespMsg.Builder respMsg = DaoMuProto.DaoMuCreateClayRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            //生成陶罐
            for (String position : reqMsg.getPositionList()) {
                int battlePropId = room.createClay(position);
                if (battlePropId > 0) {
                    DaoMuProto.DaoMuClayItem.Builder clayItem = DaoMuProto.DaoMuClayItem.newBuilder();
                    clayItem.setBattlePropId(battlePropId);
                    clayItem.setPosition(position);
                    respMsg.addClayItem(clayItem);
                }
            }
            //返回PB
            respMsg.setRet(0);
            return respMsg;
        }
    }

    /**
     * 销毁房间
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuDestroyRoomRespMsg.Builder pveDestroyRoom(DaoMuProto.DaoMuDestroyRoomReqMsg reqMsg) {
        DaoMuProto.DaoMuDestroyRoomRespMsg.Builder respMsg = DaoMuProto.DaoMuDestroyRoomRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            //返回PB
            respMsg.setRet(0);

            //判断是否盗墓还是特色服
            if (daoMuActivityConfig.getActivityInfo().getType() == eActivityType.DaoMuSpecial.getValue()) {
                //特色服结算
                boolean status = reqMsg.getStatement().getStatus();
                long battleTime = reqMsg.getStatement().getBattleTime();
                int killNum = reqMsg.getStatement().getKillNum();
                //结算
                DaoMuProto.DaoMuStatementInfoItem.Builder statement = daoMuActivityUser.overPasses(room.getPassesId(), status, room.getOfferRewardNpcIdSet(),
                        room.getLevel(), room.getSkillIds(), battleTime, killNum, room.getRoomId());
                respMsg.setStatement(statement);
                return respMsg;
            } else {
                //盗墓结算
                DaoMuProto.DaoMuStatementInfoItem.Builder statement = daoMuActivityUser.overPasses(room.getPassesId(), false, room.getOfferRewardNpcIdSet(),
                        room.getLevel(), room.getSkillIds(), room.countBattleTime(), room.countKillNpcNum(), room.getRoomId());
                respMsg.setStatement(statement);
                return respMsg;
            }
        }
    }

    /**********************************************************盗墓奇遇专用*****************************************************/

    /**
     * 盗墓奇遇-广告相关-复活
     * @param activityId
     * @return
     * @throws BusinessException
     */
    public DaoMuProto.DaoMuPveRoomPlayer doResurgence(int activityId) throws BusinessException {
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            throw BusinessException.newException(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
        }
        //获取房间
        DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
        if (room == null) {
            throw BusinessException.newException(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
        }
        //判断是否复活过
        if (room.getResurgenceNum() > 0) {
            throw BusinessException.newException(GameErrorCode.E_DAOMU_ENERGY_NOT_ENOUGH);
        }
        //复活
        room.doResurgence();
        return DaoMuActivityPb.createPveRoomPlayerPb(room).build();
    }

    /**
     * 盗墓奇遇-广告相关-添加可全选技能次数
     * @param activityId
     * @return
     * @throws BusinessException
     */
    public void doAddSkillSelectAllTime(int activityId) throws BusinessException {
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            throw BusinessException.newException(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
        }
        //获取房间
        DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
        if (room == null) {
            throw BusinessException.newException(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
        }
        //添加可全选技能次数
        room.addCanSkillSelectAllTime();
    }

    /**
     * 盗墓奇遇-看完广告-全选技能
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuSpecialPveUpgradeSkillsRespMsg.Builder specialSelectAllSkill(DaoMuProto.DaoMuSpecialPveUpgradeSkillsReqMsg reqMsg) {
        DaoMuProto.DaoMuSpecialPveUpgradeSkillsRespMsg.Builder respMsg = DaoMuProto.DaoMuSpecialPveUpgradeSkillsRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            //获取房间
            DaoMuActivityPveRoom room = daoMuActivityUser.getDaoMuActivityPveRoom();
            if (room == null || !room.getRoomId().equals(reqMsg.getRoomId())) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_ROOM_NOT_EXIST);
                return respMsg;
            }
            //判断全选次数
            if (room.getCanSkillSelectAllTime() <= 0) {
                respMsg.setRet(GameErrorCode.E_DAOMU_PVE_UPGRADE_SELECT_SKILL_ERROR);
                return respMsg;
            }
            //减少次数
            room.reSkillSelectAllTime();
            //增加已全选技能次数
            room.addSkillSelectAllTime();
            //升级
            for (int skillId : reqMsg.getSkillListList()) {
                //升级
                room.upgradeSkill(skillId, reqMsg.getMillisecond());
            }
            //返回PB
            respMsg.setRet(0);
            respMsg.setRoomPlayer(DaoMuActivityPb.createPveRoomPlayerPb(room));
            return respMsg;
        }
    }

    /**
     * 选择默认技能
     * @param reqMsg
     * @return
     */
    public DaoMuProto.DaoMuSelectDefaultSkillRespMsg.Builder selectDefaultSkill(DaoMuProto.DaoMuSelectDefaultSkillReqMsg reqMsg) {
        DaoMuProto.DaoMuSelectDefaultSkillRespMsg.Builder respMsg = DaoMuProto.DaoMuSelectDefaultSkillRespMsg.newBuilder();
        //获取活动
        int activityId = reqMsg.getActivityId();
        DaoMuActivityConfig daoMuActivityConfig = DaoMuActivityMgr.getActivityConfig(activityId);
        if (daoMuActivityConfig == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST);
            return respMsg;
        }
        //获取玩家
        DaoMuActivityUser daoMuActivityUser = daoMuActivityUserMap.get(activityId);
        if (daoMuActivityUser == null) {
            respMsg.setRet(GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST);
            return respMsg;
        }
        synchronized (daoMuActivityUser) {
            try {
                daoMuActivityUser.selectDefaultSkill(reqMsg.getSkillId());
                respMsg.setRet(0);
                respMsg.setPlayerInfo(DaoMuActivityPb.createPlayerInfoPb(daoMuActivityUser));
            } catch (BusinessException e) {
                respMsg.setRet(e.getCode());
            }
            return respMsg;
        }
    }
}
